package cn.op.portal.towel.service;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import cn.op.portal.common.vo.Page;
import cn.op.portal.towel.dao.TowelDao;
import cn.op.portal.towel.domain.Towel;

@Service()
@Transactional(propagation=Propagation.REQUIRED,isolation=Isolation.DEFAULT)
public class TowelService {
	@Resource
	private TowelDao towelDao;

	public void add(Towel towel) {
		
		String sql = "insert into t_towel(categoryId, name, productNum, description, size1, weight, price1, price2, image)"
				+ " values(?,?,?,?,?,?,?,?,?)";
		towelDao.add(sql, towel.getCategoryId(), towel.getName(), towel.getProductNum(), towel.getDescription(), towel.getSize1(),
				towel.getWeight(), towel.getPrice1(), towel.getPrice2(), towel.getImage());
	}

	public void deleteById(int id) {
		String sql = "delete from t_towel where id=?";
		towelDao.delete(sql, id);
	}

	public void update(Towel towel) {
		String sql = "update t_towel set categoryId=?, name=?, productNum=?, description=?, size1=?, weight=?, price1=?, price2=?, image=? where id=?";
		towelDao.update(sql,  towel.getCategoryId(), towel.getName(), towel.getProductNum(), towel.getDescription(), towel.getSize1(),
				towel.getWeight(), towel.getPrice1(), towel.getPrice2(), towel.getImage(), towel.getId());
	}
	
	public Towel findById(int id) {
		String sql = "select id, categoryId, name, productNum, description, size1, weight, price1, price2, image from t_towel where id=?";
		return towelDao.findById(sql, id);
	}
	
	public Page findPage(int pageNum, int pageSize) throws Exception{
		String sql = "select id, categoryId, name, productNum, description, size1, weight, price1, price2, image from t_towel ORDER BY id desc";
		return towelDao.findPage(sql, pageNum, pageSize);
	}

	public Page findPageByCategory(int pageNum, int pageSize, Object...values) throws Exception{
		String sql = "select id, categoryId, name, productNum, description, size1, weight, price1, price2, image from t_towel where categoryId=?  ORDER BY id desc";
		return towelDao.findPage(sql, pageNum, pageSize, values);
	}

	public TowelDao getTowelDao() {
		return towelDao;
	}

	public void setTowelDao(TowelDao towelDao) {
		this.towelDao = towelDao;
	}
}
